﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VisualSearch.Core;
using System.Data.SqlClient;

namespace VisualSearch.AWProduct
{
    public class ProductSearch : Provider
    {
        public override SearchSuggestion Search(string keyword)
        {
            SearchSuggestion searchSuggestion = new SearchSuggestion { Query = keyword };
            var connectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["AW"];
            using (var sqlConnection = new SqlConnection(connectionString.ConnectionString))
            {
                sqlConnection.Open();
                using (SqlCommand command = new SqlCommand("SELECT [ProductID], [Name], [ProductModel], [CultureID], [Description] FROM [Production].[vProductAndDescription] WHERE [Name] LIKE @keyword OR [Description] LIKE @keyword", sqlConnection))
                {
                    command.Parameters.Add(new SqlParameter("@keyword", String.Format("%{0}%", keyword)));
                    using (SqlDataReader reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            searchSuggestion.Section.Add(
                                new Item
                                {
                                    Id = reader.GetInt32(0).ToString(),
                                    Text = reader.GetString(1),
                                    Description = reader.GetString(4),
                                    Url = "http://localhost/#" + reader.GetInt32(0).ToString(),
                                    Image = new Image
                                    {
                                        Alt = reader.GetString(1),
                                        Source = String.Format("{0}?id={1}", base.PhotoUrl, reader.GetInt32(0)),
                                        Width = 80,
                                        Height = 49
                                    }
                                }
                            );
                        }
                    }
                }
                sqlConnection.Close();
                return searchSuggestion;
            }
        }
        public override byte[] GetPhotoStream(string Id)
        {
            var connectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["AW"];
            using (var sqlConnection = new SqlConnection(connectionString.ConnectionString))
            {
                byte[] buffer = new byte[0];
                sqlConnection.Open();
                using (SqlCommand command = new SqlCommand("SELECT c.ThumbnailPhoto, c.ThumbnailPhotoFileName FROM Production.Product a INNER JOIN Production.ProductProductPhoto b ON a.ProductID=b.ProductID INNER JOIN Production.ProductPhoto c ON b.ProductPhotoID=c.ProductPhotoID WHERE a.ProductID=@ProductID", sqlConnection))
                {
                    command.Parameters.Add(new SqlParameter("@ProductID", Id));
                    using (SqlDataReader reader = command.ExecuteReader())
                    {
                        if(reader.Read())
                        {
                            var size = reader.GetBytes(0,0,null,0,0);
                            buffer = new byte[size];
                            reader.GetBytes(0, 0, buffer, 0, (int)size);
                        }
                    }
                }
                return buffer;
                sqlConnection.Close();
            }
        
        }
    }
}
